package com.eke.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.eke.bean.Course;
import com.eke.bean.Goods;
import com.eke.bean.Users;
import com.jason.dbhelper2.jdbc.BaseJdbcTemplate;
@Repository("goodsDao")
public class GoodsDao extends BaseJdbcTemplate<Goods>{
	/**
	 * 新增商品
	 * @param goods	新增的商品
	 * @return int
	 */
	public int addGoods(Goods goods){
		return this.insert(goods);
	}
	
	/**
	 * 根据商品id查询商品
	 * @param goods_id	商品id
	 * @return {@link Goods}
	 */
	public Goods queryGoodsById(Integer goods_id){
    	return this.queryForObject("select * from goods where id = ?", goods_id);
    }
	/**
	 * 根据商品id删除商品
	 * @param goods_id
	 * @return int
	 */
	public int deleteGoods(Integer goods_id){
		return this.update("delete from goods where id = ?", goods_id);
	}
	/**
	 * 根据商品名查询商品
	 * @param goods_name
	 * @return {@link List}<{@link Goods}>
	 */
	public List<Goods> queryGoods(String goods_name){
		return this.queryForList("select * from goods where goods_name like '%?%'", goods_name);
	}
	/**
	 * 查询所有商品
	 * @return {@link List}<{@link Goods}>
	 */
	public List<Goods> queryAllGoods(){
		return this.queryForList("select * from goods where goods_state = 1");
	}
	
	/**
	 * 查询所有商品有分页
	 * @return {@link List}<{@link Goods}>
	 */
	public List<Goods> queryPagingGoods(int startRow, int pageSize){
		String sql = "SELECT t1.user_name,t1.user_photo,t1.user_id,t2.id,t2.goods_name,t2.goods_score,t2.goods_url,t2.goods_classify_id,t2.goods_integral,t2.goods_state FROM users AS t1 LEFT JOIN goods AS t2 ON t1.user_id = t2.user_id WHERE t2.goods_state = '1' ORDER BY t2.id DESC LIMIT ?,?";
		return this.queryForList(sql,startRow,pageSize);
	}
}
